﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OGS.Contracts;
using OGS.Utility;
using OGS.Web.Models.Admin;

namespace OGS.Web.Controllers.Admin
{
    public partial class AdminController : AdminBaseController
    {
        /// <summary>
        /// Content List Action
        /// </summary>
        /// <returns></returns>
        [ActionName("Content-List")]
        public ActionResult ContentList(string Name)
        {
            Name = Name == null ? string.Empty : Name.Trim();
            SearchAttributes SearchAttributes = GetSearchAttributesFromQuesryString();

            ContentListModel ContentListModel = new ContentListModel();
            ContentListModel.Populate(Name, SearchAttributes);
            ContentListModel.SearchAttributes = SearchAttributes;
            ContentListModel.Name = Name;

            return View("~/Views/Admin/Content/ContentList.aspx", ContentListModel);

        }

        /// <summary>
        /// Save Content Action Result
        /// </summary>
        /// <returns></returns>
        [ActionName("Save-Content")]
        public ActionResult SaveContent(int? ID)
        {
            SaveContentModel SaveContentModel = new SaveContentModel();
            SaveContentModel.IsActive = true;
            if (ID.HasValue)
            {
                //Get Content from DB
                SaveContentModel.GetContentDetails(ID.Value);

                SaveContentModel.IsSuccess = Session["Success"] == null ? false : true;
                Session["Success"] = null;

            }
            return View("~/Views/Admin/Content/SaveContent.aspx", SaveContentModel);
        }

        /// <summary>
        /// Save Content in DB
        /// </summary>
        /// <param name="oSaveContentModel"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        [ActionName("Save-Content")]
        public ActionResult SaveContent(SaveContentModel SaveContentModel)
        {
            if (ModelState.IsValid)
            {
                Result result = (Result)SaveContentModel.SaveContent();
                if (result == Result.Success)
                {
                    Session["Success"] = true;
                    return Redirect("/admin/save-Content/" + SaveContentModel.ID.ToString());
                }
                else if (result == Result.Duplicate)
                {
                    ModelState.AddModelError("Name", "The name already exists.");
                }
                else
                {
                    ModelState.AddModelError("Name", "Some error occurred. Please try again later.");
                }

            }
            return View("~/Views/Admin/Content/SaveContent.aspx", SaveContentModel);
        }
    }
}
